﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Hec.TextMining;
using System.Linq;
using System.Diagnostics;


namespace Tests
{
    [TestClass]
    public class TestClustering
    {
        [TestMethod]
        public void Test_Initilialisation()
        {
            var test = ObjectsCreation.TF_IDF_Weights;

            var clustering = new Clustering(3    , test);
            clustering._1_Initialization(1234);
            
        }

        [TestMethod]
        public void Test_Centroide()
        {
            var test = ObjectsCreation.TF_IDF_Weights;

            var clustering = new Clustering(3, test);
            clustering._1_Initialization(1234);
            clustering._2_CalculateCentroidOfClusters();

        }

        [TestMethod]
        public void Test_Kmeans()
        {
            var test = ObjectsCreation.TF_IDF_Weights;

            var clustering = new Clustering(3, test);
            clustering._1_Initialization(1000);
            clustering._2_CalculateCentroidOfClusters();
            clustering._3_K_Means();

        }

        [TestMethod]
        public void Test_Clustering_With_2Clusters_wanted()
        {
            var test = ObjectsCreation._5Dimension_Random_Frequencies_with_2_wanted_clusters;
            var mining = new TextMining(test);
            mining._5_rawFrequency_VectorSpaceModel(-1);
            //mining._6_discriminant(0.3, 0.7);
            mining._7_calculer_poids_tf_idf();
            mining._7_1_calculer_distances();

            var clustering = new Clustering(2, mining.VSM.ToList());
            clustering._1_Initialization(1234);
            
            var initialConfig = clustering.Clusters.First().Documents
                                    .Select(x => new {centroidid = x.Key.DistanceToCentroid.Key, name = x.Key.FullName})
                                    .OrderBy(x => x.centroidid).ThenBy(x => x.name)
                                    .ToList();

            clustering._2_CalculateCentroidOfClusters();
            clustering._3_K_Means();

            var finalConfig = clustering.Clusters
                                    .SelectMany(x => x.Documents)
                                    .Select(x => new {centroidid = x.Key.DistanceToCentroid.Key, name = x.Key.FullName})
                                    .OrderBy(x => x.centroidid).ThenBy(x => x.name)
                                    .ToList();
        }

        [TestMethod]
        public void Test_Clustering_With_3Clusters_wanted()
        {
            var test = ObjectsCreation._5Dimension_Random_Frequencies_with_3_wanted_clusters;
            var mining = new TextMining(test);
            mining._5_rawFrequency_VectorSpaceModel(-1);
            //mining._6_discriminant(0.3, 0.7);
            mining._7_calculer_poids_tf_idf();
            mining._7_1_calculer_distances();

            var clustering = new Clustering(3, mining.VSM.ToList());
            clustering._1_Initialization(1234);

            var initialConfig = clustering.Clusters.First().Documents
                                    .Select(x => new { centroidid = x.Key.DistanceToCentroid.Key, name = x.Key.FullName })
                                    .OrderBy(x => x.centroidid).ThenBy(x => x.name)
                                    .ToList();

            clustering._2_CalculateCentroidOfClusters();
            clustering._3_K_Means();

            var finalConfig = clustering.Clusters
                                    .SelectMany(x => x.Documents)
                                    .Select(x => new { centroidid = x.Key.DistanceToCentroid.Key, name = x.Key.FullName })
                                    .OrderBy(x => x.centroidid).ThenBy(x => x.name)
                                    .ToList();
        }

        [TestMethod]
        public void Test_Clustering_With_X_Clusters_wanted_full_data()
        {
            var mySerializer = new KeyValuePairSerializer(@"\MySerializedObjects\final_vsm_all.xml");
            var result = mySerializer.Deserialize();
            var mining = new TextMining(result);

            //mining._5_rawFrequency_VectorSpaceModel(-1);
            mining._6_discriminant(0.3, 0.7);
            mining._7_calculer_poids_tf_idf();
            mining._7_1_calculer_distances();

            var weightSerializer = new KeyValuePairSerializer(@"\MySerializedObjects\final_vsm_weight.xml");
            weightSerializer.Serialize(mining.VSM.ToList());

            var watch = new Stopwatch();
            watch.Start();
            var numberOfClusters = 4;
            var clustering = new Clustering(numberOfClusters, mining.VSM.ToList());
            clustering._1_Initialization(1234);

            var temp = clustering.Clusters.ToList();

            var initialConfig = temp
                                    .Select(x =>
                                        {
                                            x.Documents.Select(y =>
                                            {
                                                y.Key.DistanceToCentroid = new KeyValuePair<int, double>(x.ClusterID, -1);
                                                return y;
                                            })
                                            .ToList();
                                            return x;
                                        })
                                    .SelectMany(x => x.Documents)
                                    .Select(x => new { centroidid = x.Key.DistanceToCentroid.Key, name = x.Key.FullName })
                                    .OrderBy(x => x.centroidid).ThenBy(x => x.name)
                                    .ToList();

            clustering._2_CalculateCentroidOfClusters();
            clustering._3_K_Means();

            var finalConfig = clustering.Clusters
                                    .SelectMany(x => x.Documents)
                                    .Select(x => new { centroidid = x.Key.DistanceToCentroid.Key, name = x.Key.FullName })
                                    .OrderBy(x => x.centroidid).ThenBy(x => x.name)
                                    .ToList();
            watch.Stop();
        }

        [TestMethod]
        public void Test_Clustering_With_2Clusters_wanted_full_data()
        {
            //var mySerializer = new KeyValuePairSerializer(@"\MySerializedObjects\final_vsm_all.xml");
            //var result = mySerializer.Deserialize();
            //var mining = new TextMining(result);

            //mining._5_rawFrequency_VectorSpaceModel(-1);
            //mining._6_discriminant(0.3, 0.7);
            //mining._7_calculer_poids_tf_idf();
            //mining._7_1_calculer_distances();

            //var weightSerializer = new KeyValuePairSerializer(@"\MySerializedObjects\final_vsm_weight.xml");
            //weightSerializer.Serialize(mining.VSM.ToList());
            var mySerializer = new KeyValuePairSerializer(@"\MySerializedObjects\final_vsm_weight.xml");
            var result = mySerializer.Deserialize();

            var watch = new Stopwatch();
            watch.Start();
            var clustering = new Clustering(2, result);
            clustering._1_Initialization(1234);

            var temp = clustering.Clusters.ToList();

            var initialConfig = temp
                                    .Select(x =>
                                    {
                                        x.Documents.Select(y =>
                                        {
                                            y.Key.DistanceToCentroid = new KeyValuePair<int, double>(x.ClusterID, -1);
                                            return y;
                                        })
                                        .ToList();
                                        return x;
                                    })
                                    .SelectMany(x => x.Documents)
                                    .Select(x => new { centroidid = x.Key.DistanceToCentroid.Key, name = x.Key.FullName })
                                    .OrderBy(x => x.centroidid).ThenBy(x => x.name)
                                    .ToList();

            clustering._2_CalculateCentroidOfClusters();
            clustering._3_K_Means();

            var finalConfig = clustering.Clusters
                                    .SelectMany(x => x.Documents)
                                    .Select(x => new { centroidid = x.Key.DistanceToCentroid.Key, name = x.Key.FullName })
                                    .OrderBy(x => x.centroidid).ThenBy(x => x.name)
                                    .ToList();
            watch.Stop();
        }

        [TestMethod]
        public void Test_deserialize_big_vsm_Get_most_frequent_words()
        {
            var mySerializer = new KeyValuePairSerializer(@"\MySerializedObjects\test_vsm_big.xml");
            var result = mySerializer.Deserialize();
            var mining = new TextMining(result);
            mining._6_discriminant(0.3, 0.7);

            var order = mining.VSM
                        .SelectMany(x => x.Value)
                        //.Where(x => x.Value.Frequency > 10)
                        .OrderBy(x => x.Key)
                        .Select(x => new {key = x.Key, frequency = x.Value.Frequency})
                        .ToList();
            
        }
    }
}
